package com.javaee.lpr.service.Impl;

import com.javaee.lpr.entity.ParkSpace;
import com.javaee.lpr.mapper.ParkSpaceMapper;
import com.javaee.lpr.service.ParkSpaceService;
import com.javaee.lpr.service.ex.InsertException;
import com.javaee.lpr.service.ex.ParkSpaceNotInsertException;
import com.javaee.lpr.service.ex.UpdateException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class ParkSpaceServiceImpl implements ParkSpaceService {


    @Autowired
    private ParkSpaceMapper parkSpaceMapper;
    @Override
    public void insert(ParkSpace parkSpace) {
        Integer id=parkSpace.getId();
        ParkSpace result =parkSpaceMapper.selectById(id);
        if (result!=null) {
            throw new InsertException("车位信息已存在，无法插入");
        }
        Date date= new Date();
        parkSpace.setCreateT(date);

        Integer rows=parkSpaceMapper.insert(parkSpace);
        if (rows!=1){
            throw new ParkSpaceNotInsertException("在车位增加过程中产生了未知错误");
        }

    }

    @Override
    public void deleteById(Integer id) {
        ParkSpace result =parkSpaceMapper.selectById(id);
        if (result==null) {
            throw new ParkSpaceNotInsertException("车位信息不存在，无法删除");
        }
        Integer delete=this.parkSpaceMapper.deleteById(id);
        if (delete!=1) throw new RuntimeException("删除车位信息失败");
    }

    @Override
    public void update(ParkSpace parkSpace) {

        Integer id=parkSpace.getId();
        ParkSpace result =parkSpaceMapper.selectById(id);
        if (result!=null) {
            throw new ParkSpaceNotInsertException("车位信息不存在，无法更新");
        }
        Date date= new Date();
        parkSpace.setCreateT(date);
        Integer update=this.parkSpaceMapper.deleteById(id);
        if (update!=1) throw new UpdateException("更新车位信息失败");
    }

    @Override
    public ParkSpace selectById(Integer id) {
        ParkSpace parkSpace=parkSpaceMapper.selectById(id);
        if (parkSpace==null) {
            throw new InsertException("车位信息不存在，无法查询");
        }
        return parkSpace;
    }

    @Override
    public List<ParkSpace> selectAll() {
        List<ParkSpace> parkSpaces=parkSpaceMapper.selectAll();
        return parkSpaces;
    }
}
